﻿using Max.PH4_2102A.Shopping.Domain;
using Max.PH4_2102A.Shopping.IRespository;
using Max.PH4_2102A.Shopping.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4_2102A.Shopping.Service
{
    /// <summary>
    /// 分类的服务实现
    /// </summary>
    public class CategoryService : ICategoryService
    {
        ICategoryRespository _respository;
        /// <summary>
        /// 构造函数注入品牌仓储接口
        /// </summary>
        /// <param name="respository"></param>
        public CategoryService(ICategoryRespository respository)
        {
            _respository = respository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Delete(CategoryModel model)
        {
            return _respository.Delete(model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Categorys"></param>
        /// <returns></returns>
        public int Delete(List<CategoryModel> models)
        {
            return _respository.Delete(models);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<CategoryModel> GetAll()
        {
            return _respository.Query();
        }

        /// <summary>
        /// 根据名称查询
        /// </summary>
        /// <param name="CategoryName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<CategoryModel> GetAll(string CategoryName)
        {
            //因为这是服务层：逻辑处理。所以，所有的数据都来自于仓储层
            var list = _respository.Query();

            //再根据具体的条件进行查询
            list = list.Where(x => x.Name.Contains(CategoryName ?? ""));

            //返回查询结果
            return list;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="CategoryName"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<CategoryModel> GetAll(string CategoryName, int pageIndex, int pageSize, out int total)
        {
            var list = _respository.Query(); //所有数据 -- 设：100条
            list = list.Where(x => x.Name.Contains(CategoryName ?? "")); //根据条件去查 --- 设：查'张姓'，得结果5条

            total = list.Count(); //符合条件的总记录条数  ---- 结果的5条放到total

            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize); //对查询条件进行分页   ---对5条数据进行分页

            return list;  //返回分页后的结果数据
        }

        /// <summary>
        /// 查询、排序和分页
        /// </summary>
        /// <param name="CategoryName"></param>
        /// <param name="isAsc">(默认根据名称来排序)</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<CategoryModel> GetAll(string CategoryName, bool isAsc, int pageIndex, int pageSize, out int total)
        {
            var list = _respository.Query();

            list = list.Where(x => x.Name.Contains(CategoryName ?? ""));

            //根据名称进行升和降排序
            if (isAsc)
                list = list.OrderBy(x => x.Name);
            else
                list = list.OrderByDescending(x => x.Name);

            total = list.Count();

            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return list;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Insert(CategoryModel model)
        {
            return _respository.Insert(model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(CategoryModel model)
        {
            return _respository.Update(model);
        }


        //调用查询分类
        public List<dynamic> SelectCategory()
        {
            var data = _respository.Query().ToList();
            var result = this.SelectCategory(data, Guid.Empty.ToString());
            return result;
        }


        //查询分类(递归实现)
        private List<dynamic> SelectCategory(List<CategoryModel> data, string id)
        {
            var list = data.Where(x => id.Equals(x.ParentId.ToString()));

            List<dynamic> result = new(); //用来保存的结果
            
            list.ToList().ForEach(x =>
            {
                var items = SelectCategory(data, x.Id.ToString());  //保存子级数据，直接子级数据为null，则退出调用
                result.Add(new
                {
                    name = x.Name,
                    id = x.Id,
                    children = items == null || items.Count <= 0 ? null : items
                }) ;
            });
            return result;
        }


        private List<dynamic> 代码(string id)
        {
            var list = _respository.Query().Where(x => x.ParentId.ToString() == id);

            List<dynamic> result = new();

            foreach (var item in list)
            {
                result.Add(new { 
                    id = item.Id,
                    name = item.Name,
                    children = 代码(item.Id.ToString())
                });
            }
            return result;
        }
    }
}
